package org.myspringframework.aop.framework.adapter;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.myspringframework.aop.MethodBeforeAdvice;

//适配器模式
//把MethodBeforeAdvice包装成了一个MethodInterceptor环绕通知
public class MethodBeforeAdviceInterceptor implements MethodInterceptor {

    //前置通知
    private MethodBeforeAdvice advice;

    public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
        this.advice = advice;
    }
    public MethodBeforeAdviceInterceptor(){}

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        //前置处理  这个就是利用反射执行我们定义的前置方法
        this.advice.before(methodInvocation.getMethod(), methodInvocation.getArguments(), methodInvocation.getThis());
        //调用链条  调用了 proceed()。相当于去执行下一个methodinterceptor。类似于递归执行了，这样就行程了一个链式得调用执行
        return methodInvocation.proceed();
    }

}